/*
 * CS_REQ_EQUIP_V2.cpp
 *
 *  Created on: 2014. 4. 11.
 *      Author: zenoniaonline
 */





/*
 * CS_REQ_COSTUME_INFO.cpp
 *
 *  Created on: 2013. 10. 22.
 *      Author: mega@gamevil.com
 */

#include "../../Command.h"

#include "../../User.h"
#include "../../NLogic/Equip.h"
#include "../../NLogic/Bag.h"

namespace ProjectZ { namespace NCommand {

static void CS_REQ_EQUIP_V2_ACK(User *pUser, int res, int bag_type, uint64_t u64item_seq)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(res);
	* pAck << static_cast<uint8_t>(bag_type);
	* pAck << static_cast<uint64_t>(u64item_seq);
	pAck->MakePacket(CMD_SC_REQ_EQUIP_V2);
	pUser->Send(pAck);
}

void Command::CS_REQ_EQUIP_V2(User * pUser, ::Network::Packet * pPacket)
{
	/*
	 * // 장비 장착 요청
		CS_REQ_EQUIP_V2                                          0x0967
		{
				   uint8 equip_pos;            /// 장착위치
				   u8          u8bag_type;               /// 가방 타입
				   u64      u64item_seq;                /// 아이템 시퀀스
		}

		SC_REQ_EQUIP_V2                                          0x0968
		{
				   s16       ack;                                      /// 1:장착 완료 ,  305:시간이 만료된 아이템, 306:장착 불가 아이템
				   u8        u8bag_type;                /// 가방 타입
				   u64      u64item_seq;                /// 아이템 시퀀스
		}
	 *
	 */

	PREPARE_PACKET;
	int8_t equip_pos = 0;
	int8_t bag_type = 0;
	uint64_t u64item_seq;

	* pPacket >> equip_pos >> bag_type >> u64item_seq;

	NLogic::Bag * pBag = pUser->GetBag(static_cast<INVEN_BAG_TYPE>(bag_type));
	if (NULL == pBag)
	{
		DEBUGLOG("CS_REQ_EQUIP BAG IS NULL : %d", bag_type);
		return;
	}

	NLogic::Item * pItem = pBag->GetItem(u64item_seq);
	if (NULL == pItem)
	{
		CS_REQ_EQUIP_V2_ACK(pUser, ACK_UNKNOWN_ERROR, bag_type, u64item_seq);
		return;
	}

	// 이름표 시간 만료 확인
	if (EnumItemEquipPosition::ITEM_EQUIP_POS_NAME_TAG == static_cast<EnumItemEquipPosition>(equip_pos))
	{
		if (time(NULL) > pItem->GetData().reg_date + (pItem->GetData().eff_value[0] * 60))
		{
			CS_REQ_EQUIP_V2_ACK(pUser, ACK_TIME_EXPIRED_ITEM, bag_type, u64item_seq);
			return;
		}
	}

	if (pItem->GetData().level > (int)pUser->GetCharacterInfo().level)
	{
		CS_REQ_EQUIP_V2_ACK(pUser, ACK_NOT_EQUIPPED_ITEM, bag_type, u64item_seq);
		return;
	}

	if (false == pUser->GetEquip()->AddItem(static_cast<EnumItemEquipPosition>(equip_pos), pItem))
	{
		DEBUGLOG("%s false == pUser->GetEquip()->AddItem(static_cast<EnumItemEquipPosition>(equip_pos), pItem) EQUIP POS : %d GID : %d", __FUNCTION__, equip_pos, pUser->GetUserSeq());
		CS_REQ_EQUIP_V2_ACK(pUser, ACK_NOT_EQUIPPED_ITEM, bag_type, u64item_seq);
		return;
	}

	// 착용시 귀속 아이템인 경우
	if (pItem->GetData().buy_use == 1)
	{
		pItem->GetData().buy_use = 2;
		pItem->UpdateDatabaseItemInfo();
	}

	// 캐릭터 정보 갱신
	pUser->GetEquip()->UpdateCharacterInfo();
	pUser->FlushCharacterInfoCache();

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(ACK_OK);
	* pAck << static_cast<uint8_t>(bag_type);
	* pAck << static_cast<uint64_t>(u64item_seq);
	pAck->MakePacket(CMD_SC_REQ_EQUIP);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/


